package com.gulangyu.manage.mvc;

import com.mycompany.yourtrade.common.RedisService;
import com.mycompany.yourtrade.model.AjaxResponse;
import com.mycompany.yourtrade.model.Seller;
import com.mycompany.yourtrade.service.manage.ShopManageService;
import com.qunshang.common.SellerSession;
import com.qunshang.common.SessionUtils;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.mindrot.jbcrypt.BCrypt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author xiaoguang
 */
@Controller
@RequestMapping("/")
public class RegisterController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ShopManageService shopManageService;
    @Autowired
    private RedisService redisService;

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public ModelAndView userRegister(ModelMap model) {
        ModelAndView mv = new ModelAndView("register");
        return mv;
    }

    @RequestMapping(value = "/addSeller", method = RequestMethod.POST)
    public @ResponseBody
    AjaxResponse addSeller(HttpServletRequest request,
            @RequestParam("mobile") String mobile,
            @RequestParam("password") String password,
            @RequestParam("imageCaptcha") String imageCaptcha,
            @RequestParam("mobileCaptcha") String mobileCaptcha) {

        String id = UUID.randomUUID().toString();
        if (StringUtils.isEmpty(mobile)) {
            return new AjaxResponse(false, "手机号码不可为空");
        }
        if (StringUtils.isEmpty(imageCaptcha)) {
            return new AjaxResponse(false, "图片验证码不可为空");
        }
        if (StringUtils.isEmpty(mobileCaptcha)) {
            return new AjaxResponse(false, "手机验证码不可为空");
        }
        if (!SessionUtils.validateCaptcha(request, imageCaptcha)) {
            return new AjaxResponse(false, "图片验证码不正确");
        }
        String sessionId = request.getSession().getId();
        String redis = redisService.getValueOps().get(sessionId);
        if(redis == null){
            return new AjaxResponse(false, "手机验证码已失效，请重新发送验证码");
        }
        if (!mobileCaptcha.equals(redis)) {
            return new AjaxResponse(false, "手机验证码不正确");
        }
        
        Seller sell = new Seller();
        sell.setId(id);
        sell.setMobile(mobile);

        try {
            Integer result = shopManageService.addSeller(id, mobile, BCrypt.hashpw(password, BCrypt.gensalt(10)));
            if (result > 0) {
                // 创建商户Session
                SellerSession session = new SellerSession(sell);
                SessionUtils.setSession(request, session);
                logger.info("seller created. [mobile={}]", sell.getMobile());
                return new AjaxResponse(true);
            } else {
                logger.error("seller create failed. [mobile={}]", sell.getMobile());
                return new AjaxResponse(false, "保存失败");
            }
        } catch (Exception e) {
            logger.error("seller create failed. [mobile={},reason ={}]", sell.getMobile(), e.getMessage());
            return new AjaxResponse(false, "系统错误");
        }

    }
}
